package baizhi.service.Impl;

import baizhi.dao.StudentDao;
import baizhi.entity.Student;
import baizhi.service.StudentService;
import baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public void add(Student s) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            StudentDao dao = sqlSession.getMapper(StudentDao.class);
            dao.insert(s);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public void removeOne(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            StudentDao dao = sqlSession.getMapper(StudentDao.class);
            dao.delete(id);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public void removeAny(Integer[] ids) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            StudentDao dao = sqlSession.getMapper(StudentDao.class);
            dao.deleteAny(ids);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public void updateOne(Student s) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try {
            StudentDao dao = sqlSession.getMapper(StudentDao.class);
            dao.update(s);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public Student getOne(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student s = dao.selectOne(id);
        sqlSession.close();
        return s;
    }

    @Override
    public List<Student> selectPage(Integer pageNum, Integer pageSize, String name, Integer age1, Integer age2) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Integer start = (pageNum-1)*pageNum;
        List<Student> list = dao.selectPage(start, pageSize, name, age1, age2);
        sqlSession.close();
        return list;
    }

    @Override
    public Integer total(String name, Integer age1, Integer age2) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Integer count = dao.count(name, age1, age2);
        sqlSession.close();
        return count;
    }
}
